import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'dva';
import { Redirect, Switch, Route } from 'dva/router';
import { getRoutes } from '../utils/utils';
// import Authorized from '../utils/Authorized';
// import { getMenuData } from '../common/menu';
import Tabbar from '../routes/tabBar';

// const { AuthorizedRoute } = Authorized;

/**
 * 根据菜单取得重定向地址.
 */
const redirectData = [];

class BasicLayout extends React.PureComponent {
  static childContextTypes = {
    location: PropTypes.object,
    breadcrumbNameMap: PropTypes.object,
  };

  // constructor(props) {
  //   super(props);
  //   this.state = {
  //     height: 2,
  //   };
  // }
  getChildContext() {
    const { location, routerData } = this.props;
    return {
      location,
      breadcrumbNameMap: routerData,
    };
  }
  componentDidMount() {
  }
  getBashRedirect = () => {
    // According to the url parameter to redirect
    // 这里是重定向的,重定向到 url 的 redirect 参数所示地址
    const urlParams = new URL(window.location.href);

    const redirect = urlParams.searchParams.get('redirect');
    // Remove the parameters in the url
    if (redirect) {
      urlParams.searchParams.delete('redirect');
      window.history.replaceState(null, 'redirect', urlParams.href);
    } else {
      return '/internaltradetype';
    }
    return redirect;
  };
  render() {
    const {
      routerData, match,
    } = this.props;
    const bashRedirect = this.getBashRedirect();

    const tabbarRouter = (
      <Switch>
        {
          redirectData.map(item =>
            <Redirect key={item.from} exact from={item.from} to={item.to} />
          )
        }
        {
          getRoutes(match.path, routerData).map(item =>
            (
              <Route
                path={item.path}
                component={item.component}
                key={item.key}
              />
            )
          )
        }
        <Redirect exact from="/" to={bashRedirect} />
      </Switch>
    );
    return (
      <div>
        <Tabbar dispatch={this.props.dispatch} tabbarRouter={tabbarRouter} />
      </div>
    );
  }
}

export default connect(({ global }) => ({
  collapsed: global.collapsed,
}))(BasicLayout);
